package com.seadragon.app.dms.service.impl;import static com.seadragon.app.dms.model.DmsConstants.CUSTOMER_STATUS_ACTIVE;import static com.seadragon.app.dms.model.DmsConstants.PERIOD_TO_EXPIRE_RESET_PASSWORD_LINK;import java.util.Calendar;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.UUID;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;import com.seadragon.app.dms.dao.CustomerDao;import com.seadragon.app.dms.model.Address;import com.seadragon.app.dms.model.CreditCard;import com.seadragon.app.dms.model.Customer;import com.seadragon.app.dms.service.CustomerService;
@Repository
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerDao customerDao;
	
	@Override
	public Customer update(Customer customer) {
		Customer customer2 = customerDao.findByUserName(customer.getUserName());
		if(customer.getFirstName() != null && customer.getFirstName().length() > 0 
				&& !customer.getFirstName().equalsIgnoreCase(customer2.getFirstName())){
			customer2.setFirstName(customer.getFirstName());
		}
		if(customer.getLastName() != null && customer.getLastName().length() > 0 
				&& !customer.getLastName().equalsIgnoreCase(customer2.getLastName())){
			customer2.setLastName(customer.getLastName());
		}
		boolean addressChanged = false;
		Address residentAddress = customer.getResidentAddress();
		Address residentAddress2 = customer2.getResidentAddress();
		if(residentAddress.getAddressLine1() != null && residentAddress.getAddressLine1().length() > 0 
				&& !residentAddress.getAddressLine1().equalsIgnoreCase(residentAddress2.getAddressLine1())){
			residentAddress2.setAddressLine1(residentAddress.getAddressLine1());
			addressChanged = true;
		}
		if(residentAddress.getAddressLine2() != null && residentAddress.getAddressLine2().length() > 0 
				&& !residentAddress.getAddressLine2().equalsIgnoreCase(residentAddress2.getAddressLine2())){
			residentAddress2.setAddressLine2(residentAddress.getAddressLine2());
			addressChanged = true;
		}
		if(residentAddress.getCity() != null && residentAddress.getCity().length() > 0 
				&& !residentAddress.getCity().equalsIgnoreCase(residentAddress2.getCity())){
			residentAddress2.setCity(residentAddress.getCity());
			addressChanged = true;
		}
		if(residentAddress.getState() != null && residentAddress.getState().length() > 0 
				&& !residentAddress.getState().equalsIgnoreCase(residentAddress2.getState())){
			residentAddress2.setState(residentAddress.getState());
			addressChanged = true;
		}
		if(residentAddress.getZip() != null && residentAddress.getZip().length() > 0 
				&& !residentAddress.getZip().equalsIgnoreCase(residentAddress2.getZip())){
			residentAddress2.setZip(residentAddress.getZip());
			addressChanged = true;
		}
		if(addressChanged){
			residentAddress2.setLatitude(0);
			residentAddress2.setLongitude(0);
		}
		if(customer.getGender() != null && customer.getGender().length() > 0 
				&& !customer.getGender().equalsIgnoreCase(customer2.getGender())){
			customer2.setGender(customer.getGender());
		}
		if(customer.getPhoneNumber() != null && customer.getPhoneNumber().length() > 0 
				&& !customer.getPhoneNumber().equalsIgnoreCase(customer2.getPhoneNumber())){
			customer2.setPhoneNumber(customer.getPhoneNumber());
		}
		if(customer.getLanguage() != null && customer.getLanguage().length() > 0 
				&& !customer.getLanguage().equalsIgnoreCase(customer2.getLanguage())){
			customer2.setLanguage(customer.getLanguage());
		}
		if(customer.getDateOfBirth() != null && (customer2.getDateOfBirth() == null || customer.getDateOfBirth().compareTo(customer2.getDateOfBirth()) != 0)){
			customer2.setDateOfBirth(customer.getDateOfBirth());
		}		Set<CreditCard> creditCards = customer.getCreditCards();		Set<CreditCard> creditCards2 = customer2.getCreditCards();		if(creditCards == null || creditCards.size() == 0){			creditCards2.clear();		} else {			Map<String, CreditCard> ccMap = new HashMap<String, CreditCard>();			Iterator<CreditCard> iter = creditCards.iterator();			while(iter.hasNext()){				CreditCard cc = iter.next();				ccMap.put(cc.getAccountNumber(), cc);			}			iter = customer2.getCreditCards().iterator();						while(iter.hasNext()){				CreditCard cc = iter.next();				if(ccMap.get(cc.getAccountNumber()) == null){					iter.remove();				}else{					ccMap.remove(cc.getAccountNumber());				}			}			Iterator<String> accountIter = ccMap.keySet().iterator();			while(accountIter.hasNext()){				creditCards2.add(ccMap.get(accountIter.next()));			}		}
		customerDao.update(customer2);		return customer2;
	}

	@Override
	public void activate(UUID token){
		Customer customer = customerDao.findByToken(token.toString());
		customer.setStatus(CUSTOMER_STATUS_ACTIVE);
		customer.setToken(null);
		customerDao.save(customer);
	}
	
	@Override
	public void save(Customer customer) {
//		Calendar now = Calendar.getInstance();
//		if(customer.getToken() != null && customer.getTimeToExpire().before(now.getTime())){
//			customer.setToken(null);
//			customer.setTimeToExpire(null);
//		}
		customerDao.save(customer);
	}

	@Override
	public Customer findByUserName(String userName){
		return customerDao.findByUserName(userName);
	}
	
	@Override
	public Customer resetPassword(String userName){
		Customer customer = customerDao.findByUserName(userName);
		if(customer != null){
			customer.setToken(UUID.randomUUID().toString());
			Calendar now = Calendar.getInstance();
			now.add(Calendar.MINUTE, PERIOD_TO_EXPIRE_RESET_PASSWORD_LINK);
			customer.setTimeToExpire(now.getTime());
			customerDao.save(customer);
		}
		return customer;
	}
}
